package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.entity.Area;
import com.shike.hq.entity.Group;
import com.shike.hq.entity.Inspection;
import com.shike.hq.service.AreaService;
import com.shike.hq.service.GroupService;
import com.shike.hq.service.InspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

/**
 * The type Inspection controller.
 */
@Controller
@RequestMapping("/base")
public class InspectionController {

  private static final String REDIRECT = "redirect:/base/inspection";
  @Autowired
  InspectionService inspectionService;
  @Autowired
  GroupService groupService;
  @Autowired
  AreaService areaService;

  /**
   * 巡检员页面路径映射
   *
   * @param model   model类型
   * @param session session类型
   * @return 页面路径映射字段
   */
  @GetMapping("/inspection")
  public String inspection(Model model, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Inspection> inspections = inspectionService.selectAllInspectionByAreaId(areaId);
    if (inspections.size() > 0) {
      //找到巡检员信息
      model.addAttribute("inspections", inspections);
    } else {
      model.addAttribute("inspections", "null");
    }
    ArrayList<Area> areas = areaService.selectAllAreaById(areaId);  //区域
    model.addAttribute("areas", areas);
    ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);  //巡检组
    model.addAttribute("groups", groups);
    return "base/inspection";
  }

  /**
   * 巡检组页面路径映射
   *
   * @param model   model类型
   * @param session session类型
   * @return 页面路径映射字段
   */
  @GetMapping("/inspection/group")
  public String getGroup(Model model, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);
    if (groups.size() > 0) {
      //找到巡检组信息
      model.addAttribute("groups", groups);
    } else {
      model.addAttribute("groups", "Oops……没有数据呀~");
    }
    return "base/inspection";
  }

  /**
   * 编辑巡检员信息
   *
   * @param inspection 编辑后的巡检员信息
   * @param attributes      model类型
   * @return 页面路径字符串
   */
  @PostMapping("/inspection/update")
  public String updateInspection(Inspection inspection, RedirectAttributes attributes) {
    try {
      inspectionService.updateById(inspection);
      attributes.addAttribute("msg", "编辑成功！");
    } catch (Exception e){
      attributes.addAttribute("msg", "编辑失败！");
    }
    return REDIRECT;
  }

  /**
   * 删除巡检员信息
   *
   * @param id         要删除的巡检员id
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/inspection/delete")
  public String deleteInspection(@RequestParam("id") long id, RedirectAttributes attributes) {
    try {
      inspectionService.removeById(id);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！");
    }
    return REDIRECT;
  }

  /**
   * 删除多条巡检员信息
   *
   * @param idList     巡检员id列表
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/inspection/deleteInspections")
  public String deleteInspections(@RequestParam("idList") ArrayList<Long> idList,
                                  RedirectAttributes attributes) {
    try {
      inspectionService.removeByIds(idList);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！");
    }
    return REDIRECT;
  }

  /**
   * 添加巡检员信息
   *
   * @param inspection 添加的巡检员信息
   * @param attributes      model类型
   * @return 页面路径字符串
   */
  @PostMapping("/inspection/add")
  public String addInspection(Inspection inspection, RedirectAttributes attributes) {
    inspection.setCreateTime(new Timestamp(System.currentTimeMillis()));
    try {
      inspectionService.save(inspection);
      attributes.addFlashAttribute("msg", "添加成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "添加失败！");
    }
    return REDIRECT;
  }

  /**
   * 指定巡检组进行页面映射.
   *
   * @param model model类型
   * @return 页面路径字符串 string
   */
  @PostMapping("/inspection/showRefresh")
  public String showRefresh(Model model, @RequestParam("ParentId") long ParentId) {
    ArrayList<Inspection> list = inspectionService.selectInspectionsByGroupId(ParentId);
    model.addAttribute("inspections", list);
    return "/base/inspection::showRefresh";
  }
}
